/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.una.accidentmanagement.bo;

import br.com.una.accidentmanagement.exception.InsertException;
import br.com.una.accidentmanagement.exception.UpdateException;
import br.com.una.accidentmanagement.model.*;
import br.com.una.accidentmanagement.util.DatabaseConnection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

/**
 *
 * @author emilianoeloi
 */
public class TicketController {
    
        public List<Ticket> getAllTickets(){
            EntityManager em = DatabaseConnection.instance().getManager();
            TypedQuery<Ticket> query = em.createQuery("SELECT t FROM Ticket t", Ticket.class);        
            return query.getResultList();
        }
        
        public List<Ticket> getTicketByField(String campo, String valor){
            EntityManager em = DatabaseConnection.instance().getManager();
            TypedQuery<Ticket> query = em.createQuery("SELECT t FROM Ticket t where t."+campo+" like :"+campo, Ticket.class);                                
            query.setParameter(campo, "%"+valor+"%");
            return query.getResultList();
        }
        
        public void deleteTicket(Ticket t){
            EntityManager em = DatabaseConnection.instance().getManager();
            EntityTransaction et = em.getTransaction();
            et.begin();
            em.remove(t);
            et.commit();
        }
        
        public Long getCountTickets(){
            String jpql = "SELECT count(o) FROM Ticket o";  
            Query q = DatabaseConnection.instance().getManager().createQuery(jpql);  
            Long count = (Long) q.getSingleResult();  
            return count;
        }

	public List<Ticket> getTicketsByStatus(int statusId) {
            EntityManager em = DatabaseConnection.instance().getManager();            
            CriteriaBuilder cb =em.getCriteriaBuilder();
            CriteriaQuery cq = cb.createQuery(Ticket.class);            
            TypedQuery<Ticket> query = em.createQuery(
            "SELECT t FROM Ticket t WHERE t.statusId = :statusId", Ticket.class);            
            return query.setParameter("name", statusId).getResultList();
	}

	public Ticket getTicketById(int ticketId) {
            EntityManager em = DatabaseConnection.instance().getManager();            
            CriteriaBuilder cb =em.getCriteriaBuilder();     
            TypedQuery<Ticket> query = em.createQuery(
            "SELECT t FROM Ticket t WHERE t.ticketId = :ticketId", Ticket.class);            
            return query.setParameter("name", ticketId).getSingleResult();
	}

	public void updateTicket(Ticket ticket){
            EntityManager em = DatabaseConnection.instance().getManager();            
            EntityTransaction et = em.getTransaction();
            et.begin(); 
            em.merge(ticket);
            et.commit();
	}

	public void insertTicket(Ticket ticket){
            EntityManager em = DatabaseConnection.instance().getManager();                        
            EntityTransaction et = em.getTransaction();
            et.begin();
            em.persist(ticket);
            et.commit();
	}
                
                

}
